package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseLand;
import top.jnaw.jee.utils.ApprovalUtil;
import top.jnaw.jee.utils.Dates;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Land extends BaseLand<Land> {

  public static final Land dao = new Land().dao();

  public static ModelResult add(String openid, int did, String data) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    WechatUsers wechatUsers = WechatUsers.getByOpenid(openid);
    if (null != wechatUsers) {
      JSONArray process = Department.getProcess(did);
      result = Approval.add(wechatUsers.getId(), 1, "土地经营权证申请", process, null);
    }
    if (result.fine()) {
      Approval approval = (Approval) result.getObject();
      if (null != approval) {
        Land land = new Land();
        land.setCreateTime(Dates.now());
        land.setData(data);
        land.setOpenid(openid);
        land.setDid(did);
        land.setAid(approval.getId());
        if (land.save()) {
          result.setResult(Result.kSuccess);
          result.setObject(land);
        }
      }
    }
    return result;
  }

  public static JSONObject list(int page, int size, JSONArray filter, int uid) {
    String sec = Db.getSql("land-list-sec");
    String exp = Db.getSql("land-list-exp");
    Object openid = Strings.get(filter, "openid", false);
    Object status = Strings.get(filter, "status", false);
    if (null != openid) {
      exp += " AND l.openid = '" + openid.toString() + "'";
    }
    if (uid > 0) {
      exp += " AND ar.approver= " + uid;
    }
    if (null != status) {
      if (Consts.APPROVAL_CHECKED.equals(status.toString())) {
        exp += " AND ar.status !='cancel' AND ar.status !='checking'";
      } else {
        exp += " AND l.status = '" + status.toString() + "'";
      }
    }
    exp += " order by l.create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = null;
    String name = Consts.APPROVAL_LAND_FIRST_DNAME;
    String sql = Db.getSql("land-query");
    List<Land> list = dao.find(sql, id);
    if (null != list && list.size() > 0) {
      object = Models.compressList(list);
      int total = object.getInteger("total");
      String data = object.getString("data");
      JSONObject department = object.getJSONObject("department");
      if (null != department) {
        name = department.getString("title");
      }
      JSONArray reply = object.getJSONArray("reply");
      if (null != reply && reply.size() == total) {
        reply.getJSONObject(0).put("dname", name);
        reply.getJSONObject(1).put("dname", Consts.APPROVAL_LAND_SECOND_DNAME);
      }
      object.put("data", ApprovalUtil.charge(data));
    }
    if (null != object) {
      result.setObject(object);
      result.setResult(Result.kSuccess);
    }
    return result;
  }

  public static ModelResult updateStatus(int aid, String status) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);
    Land land = dao.findFirst("select * from land where aid=?", aid);
    if (null != land) {
      land.setStatus(status);
      boolean r = land.update();
      if (r) {
        result.setObject(land);
        result.setResult(Result.kSuccess);
      }
    }
    return result;
  }
}
